/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DevicesBox.java
 *
 * Created on 6-jan-2011, 18:41:12
 */

package nl.lemval.nododue.dialog;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.JComponent;
import javax.swing.KeyStroke;
import javax.swing.ListCellRenderer;
import nl.lemval.nododue.Options;
import nl.lemval.nododue.component.DevicesListModel;
import nl.lemval.nododue.util.Device;
import org.jdesktop.application.Action;

/**
 *
 * @author Michael
 */
public class DevicesBox extends javax.swing.JDialog {

    private DevicesListModel signalModel;
    private ListCellRenderer signalCellRenderer;
    private Device current;

    /** Creates new form DevicesBox */
    public DevicesBox(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        prepare();
        initComponents();

        discoverCheck.setSelected(Options.getInstance().isScanResponse());
    }

    private void prepare() {
        signalModel = new DevicesListModel();
        signalCellRenderer = new DevicesCellRenderer();

        KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
        getRootPane().registerKeyboardAction(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                close();
            }
        }, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        listScrollPanel = new javax.swing.JScrollPane();
        signalList = new javax.swing.JList();
        listLabel = new javax.swing.JLabel();
        buttonPanel = new javax.swing.JPanel();
        syncButton = new javax.swing.JButton();
        closeButton = new javax.swing.JButton();
        detailPanel = new javax.swing.JPanel();
        nameLabel = new javax.swing.JLabel();
        nameField = new javax.swing.JTextField();
        locationLabel = new javax.swing.JLabel();
        locationField = new javax.swing.JTextField();
        sourceLabel = new javax.swing.JLabel();
        sourceField = new javax.swing.JTextField();
        ignoreLabel = new javax.swing.JLabel();
        ignoreField = new javax.swing.JCheckBox();
        jPanel1 = new javax.swing.JPanel();
        discoverCheck = new javax.swing.JCheckBox();
        hideDisabledCheck = new javax.swing.JCheckBox();

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(nl.lemval.nododue.NodoDueManager.class).getContext().getResourceMap(DevicesBox.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setMinimumSize(new java.awt.Dimension(456, 370));
        setName("Form"); // NOI18N

        listScrollPanel.setName("listScrollPanel"); // NOI18N

        signalList.setModel(signalModel);
        signalList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        signalList.setCellRenderer(signalCellRenderer);
        signalList.setName("signalList"); // NOI18N
        signalList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                selectionChanged(evt);
            }
        });
        listScrollPanel.setViewportView(signalList);

        listLabel.setFont(listLabel.getFont().deriveFont(listLabel.getFont().getStyle() | java.awt.Font.BOLD));
        listLabel.setText(resourceMap.getString("listLabel.text")); // NOI18N
        listLabel.setName("listLabel"); // NOI18N

        buttonPanel.setName("buttonPanel"); // NOI18N
        buttonPanel.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(nl.lemval.nododue.NodoDueManager.class).getContext().getActionMap(DevicesBox.class, this);
        syncButton.setAction(actionMap.get("synchronize")); // NOI18N
        syncButton.setText(resourceMap.getString("syncButton.text")); // NOI18N
        syncButton.setName("syncButton"); // NOI18N
        buttonPanel.add(syncButton);

        closeButton.setAction(actionMap.get("close")); // NOI18N
        closeButton.setText(resourceMap.getString("closeButton.text")); // NOI18N
        closeButton.setName("closeButton"); // NOI18N
        buttonPanel.add(closeButton);

        detailPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        detailPanel.setName("detailPanel"); // NOI18N

        nameLabel.setText(resourceMap.getString("nameLabel.text")); // NOI18N
        nameLabel.setName("nameLabel"); // NOI18N

        nameField.setText(resourceMap.getString("nameField.text")); // NOI18N
        nameField.setName("nameField"); // NOI18N
        nameField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                updateDevice(evt);
            }
        });

        locationLabel.setText(resourceMap.getString("locationLabel.text")); // NOI18N
        locationLabel.setName("locationLabel"); // NOI18N

        locationField.setText(resourceMap.getString("locationField.text")); // NOI18N
        locationField.setName("locationField"); // NOI18N
        locationField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                updateDevice(evt);
            }
        });

        sourceLabel.setText(resourceMap.getString("sourceLabel.text")); // NOI18N
        sourceLabel.setName("sourceLabel"); // NOI18N

        sourceField.setEditable(false);
        sourceField.setText(resourceMap.getString("sourceField.text")); // NOI18N
        sourceField.setName("sourceField"); // NOI18N

        ignoreLabel.setText(resourceMap.getString("ignoreLabel.text")); // NOI18N
        ignoreLabel.setName("ignoreLabel"); // NOI18N

        ignoreField.setText(resourceMap.getString("ignoreField.text")); // NOI18N
        ignoreField.setMargin(new java.awt.Insets(0, 0, 0, 0));
        ignoreField.setName("ignoreField"); // NOI18N
        ignoreField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                updateDevice(evt);
            }
        });

        javax.swing.GroupLayout detailPanelLayout = new javax.swing.GroupLayout(detailPanel);
        detailPanel.setLayout(detailPanelLayout);
        detailPanelLayout.setHorizontalGroup(
            detailPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(detailPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(detailPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(detailPanelLayout.createSequentialGroup()
                        .addComponent(nameLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 40, Short.MAX_VALUE)
                        .addComponent(nameField, javax.swing.GroupLayout.PREFERRED_SIZE, 171, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, detailPanelLayout.createSequentialGroup()
                        .addGroup(detailPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(locationLabel)
                            .addComponent(sourceLabel)
                            .addComponent(ignoreLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 25, Short.MAX_VALUE)
                        .addGroup(detailPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(ignoreField)
                            .addGroup(detailPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(sourceField)
                                .addComponent(locationField, javax.swing.GroupLayout.DEFAULT_SIZE, 171, Short.MAX_VALUE)))))
                .addContainerGap())
        );
        detailPanelLayout.setVerticalGroup(
            detailPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(detailPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(detailPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nameLabel)
                    .addComponent(nameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(detailPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(locationLabel)
                    .addComponent(locationField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(detailPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(sourceField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(sourceLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(detailPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ignoreLabel)
                    .addComponent(ignoreField))
                .addContainerGap(55, Short.MAX_VALUE))
        );

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel1.setName("jPanel1"); // NOI18N
        jPanel1.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        discoverCheck.setText(resourceMap.getString("discoverCheck.text")); // NOI18N
        discoverCheck.setMargin(new java.awt.Insets(0, 0, 0, 0));
        discoverCheck.setName("discoverCheck"); // NOI18N
        discoverCheck.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                discoverDevices(evt);
            }
        });
        jPanel1.add(discoverCheck);

        hideDisabledCheck.setAction(actionMap.get("showOrHideDisabledDevices")); // NOI18N
        hideDisabledCheck.setText(resourceMap.getString("hideDisabledCheck.text")); // NOI18N
        hideDisabledCheck.setMargin(new java.awt.Insets(0, 0, 0, 0));
        hideDisabledCheck.setName("hideDisabledCheck"); // NOI18N
        hideDisabledCheck.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                hideDisabledCheckdiscoverDevices(evt);
            }
        });
        jPanel1.add(hideDisabledCheck);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(listScrollPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 148, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 277, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(detailPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(buttonPanel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 432, Short.MAX_VALUE)
                    .addComponent(listLabel))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(listLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(listScrollPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 261, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(detailPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 18, Short.MAX_VALUE)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(buttonPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void selectionChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_selectionChanged
        if (evt.getValueIsAdjusting() == false) {
            Device device = getSelectedDevice();
            updateDevice(null);
            if ( device != null ) {
                nameField.setText(device.getName());
                locationField.setText(device.getLocation());
                sourceField.setText(device.getSource());
                ignoreField.setSelected(device.isIgnored());
                current = device;
            }
        }
    }

    @Action
    public void close() {
        synchronize();
        setVisible(false);
    }//GEN-LAST:event_selectionChanged

    private void updateDevice(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_updateDevice
        if ( current != null ) {
            current.setName(nameField.getText());
            current.setLocation(locationField.getText());
            current.setIgnored(ignoreField.isSelected());
        }
    }//GEN-LAST:event_updateDevice

    private void discoverDevices(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_discoverDevices
        Options.getInstance().setScanResponse(discoverCheck.isSelected());
    }//GEN-LAST:event_discoverDevices

    private void hideDisabledCheckdiscoverDevices(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_hideDisabledCheckdiscoverDevices
        // TODO add your handling code here:
    }//GEN-LAST:event_hideDisabledCheckdiscoverDevices

    private Device getSelectedDevice() {
        Object[] selected = signalList.getSelectedValues();
        if ( selected.length == 1 ) {
            return (Device) selected[0];
        }
        return null;
    }

    @Action
    public void synchronize() {
        signalModel.synchronize();
    }

    @Action
    public void showOrHideDisabledDevices() {
        signalModel.hideDisabled(hideDisabledCheck.isSelected());
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel buttonPanel;
    private javax.swing.JButton closeButton;
    private javax.swing.JPanel detailPanel;
    private javax.swing.JCheckBox discoverCheck;
    private javax.swing.JCheckBox hideDisabledCheck;
    private javax.swing.JCheckBox ignoreField;
    private javax.swing.JLabel ignoreLabel;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel listLabel;
    private javax.swing.JScrollPane listScrollPanel;
    private javax.swing.JTextField locationField;
    private javax.swing.JLabel locationLabel;
    private javax.swing.JTextField nameField;
    private javax.swing.JLabel nameLabel;
    private javax.swing.JList signalList;
    private javax.swing.JTextField sourceField;
    private javax.swing.JLabel sourceLabel;
    private javax.swing.JButton syncButton;
    // End of variables declaration//GEN-END:variables

}
